namespace GameHelper.Saving
{
    using System;
    using System.Text;
    using System.Collections.Generic;
    using UnityEngine;

    /// <summary>
    ///   Unity PlayerPrefs扩展
    /// </summary>
    public static partial class Persist
    {
#region 封装

        public static void DeleteAll()
        {
            PlayerPrefs.DeleteAll();
        }

        public static void DeleteKey(string key)
        {
            PlayerPrefs.DeleteKey(key);
        }

        public static float GetFloat(string key, float defaultValue)
        {
            return PlayerPrefs.GetFloat(key, defaultValue);
        }

        public static float GetFloat(string key)
        {
            // return PlayerPrefs.GetFloat(key, 0.0f);
            return GetFloat(key, 0.0f);
        }

        public static int GetInt(string key, int defaultValue)
        {
            return PlayerPrefs.GetInt(key, defaultValue);
        }

        public static int GetInt(string key)
        {
            // return PlayerPrefs.GetInt(key, 0);
            return GetInt(key, 0);
        }

        public static string GetString(string key, string defaultValue)
        {
            return PlayerPrefs.GetString(key, defaultValue);
        }

        public static string GetString(string key)
        {
            // return PlayerPrefs.GetString(key, null);
            return GetString(key, null);
        }

        public static bool HasKey(string key)
        {
            return PlayerPrefs.HasKey(key);
        }

        public static void Save()
        {
            PlayerPrefs.Save();
        }

        public static void SetFloat(string key, float value)
        {
            PlayerPrefs.SetFloat(key, value);
        }

        public static void SetInt(string key, int value)
        {
            PlayerPrefs.SetInt(key, value);
        }

        public static void SetString(string key, string value)
        {
            if (null == value)
            {
                PlayerPrefs.DeleteKey(key);
            }else{
                PlayerPrefs.SetString(key, value);
            }
        }

#endregion

#region 扩展

        public static bool GetBool(string key, bool defaultValue = false)
        {
            if (HasKey(key))
            {
                return Convert.ToBoolean(GetInt(key));
            }
            return defaultValue;
        }

        public static void SetBool(string key, bool value)
        {
            SetInt(key, Convert.ToInt32(value));
        }

        public static long GetInt64(string key, long defaultValue = 0L)
        {
            if (HasKey(key))
            {
                var s = GetString(key);
                long result;
                if (long.TryParse(s, out result))
                {
                    return result;
                }
            }
            return defaultValue;
        }

        public static void SetInt64(string key, long value)
        {
            SetString(key, value.ToString());
        }

        public static ulong GetUInt64(string key, ulong defaultValue = 0L)
        {
            if (HasKey(key))
            {
                var s = GetString(key);
                ulong result;
                if (ulong.TryParse(s, out result))
                {
                    return result;
                }
            }
            return defaultValue;
        }

        public static void SetUInt64(string key, ulong value)
        {
            SetString(key, value.ToString());
        }

        public static DateTime GetDateTime(string key)
        {
            var v = GetInt64(key, 0L);
            return DateTime.FromBinary(v);
        }

        public static void SetDateTime(string key, DateTime value)
        {
            SetInt64(key, value.ToBinary());
        }

        public static double GetDouble(string key, double defaultValue = 0.0)
        {
            if (HasKey(key))
            {
                var s = GetString(key);
                double result;
                if (double.TryParse(s, out result))
                {
                    return result;
                }
            }
            return defaultValue;
        }

        public static void SetDouble(string key, double value)
        {
            SetString(key, value.ToString());
        }

#endregion
    }
}
